import { getSelectCollection, getMultiSelectCollection } from '@/api/common'

import * as _enumNames from '@/contracts/enumFullNames'
import * as _constantNames from '@/contracts/constantNames'

let createNameArray = (nameModule, category, group, prefix, suffix) => {
  let result = []
  prefix = prefix ? prefix : group
  suffix = suffix ? suffix : category
  for (const key in nameModule) {
    if (Object.hasOwnProperty.call(nameModule, key) && nameModule[key] && typeof nameModule[key] == 'string') {
      result.push({
        name: nameModule[key],
        category: category,
        group: group,
        keyName: key.toString(),
        keyPrefix: prefix,
        keySuffix: suffix,
      })
    }
  }

  return result
}

let createNameInstance = (arr) => {
  let result = {}
  for (const nameItem of arr) {
    var key = nameItem.keyName
    var oldItem = result[key]
    var tryCount = 4
    while (tryCount-- > 0 && oldItem) {
      //同组（同站点）
      if (oldItem.group == nameItem.group) {
        key += nameItem.keySuffix || oldItem.category
      } else {
        key = (nameItem.keyPrefix || nameItem.group) + key
      }
      oldItem = result[key]
    }
    if (!oldItem) {
      result[key] = nameItem
    }
  }

  return result
}

let _apiOptions = {
  book: {
    getSelectCollection: getSelectCollection,
    getMultiSelectCollection: getMultiSelectCollection,
  },
}

let _allNameArray = [
  ...createNameArray(_enumNames, 'Enum', 'book'),
  ...createNameArray(_constantNames, 'Constant', 'book'),
]

let _allNames = createNameInstance(_allNameArray)

let getSelectOptions = (name, args) => {
  var apiOptions = _apiOptions[name.group]
  var key = name.name
  var category = name.category
  if (!apiOptions || !key || !category) return null

  return apiOptions.getSelectCollection(key, category, args).then((res) => {
    res.data = res.data.items ? res.data.items : res.data
    return res
  })
}

let getMultiSelectOptions = (...names) => {
  let siteMap = new Map()

  names.forEach((item) => {
    var apiOptions = _apiOptions[item.group]
    var g = item.group
    var key = item.name
    var category = item.category
    if (!apiOptions || !key || !category) return
    let m
    if (siteMap.has(g)) {
      m = siteMap.get(g)
    } else {
      m = {
        group: g,
        names: [],
        getMultiSelectCollection: apiOptions.getMultiSelectCollection,
      }
      siteMap.set(g, m)
    }
    m.names.push({ key: key, category: category })
  })

  let ps = []
  for (var [g, apiOptions] of siteMap.entries()) {
    let _g = g
    ps.push(
      apiOptions
        .getMultiSelectCollection(apiOptions.names)
        .then((res) => {
          res._group = _g
          return res
        })
        .catch((e) => e),
    )
  }

  let p = Promise.all(ps).then((arr) => {
    var dataMaps = {}
    arr.forEach((res) => {
      dataMaps[res._group] = res.data
    })

    let result = {
      isSuccess: true,
      data: arr,
      _dataMaps: dataMaps,
      getOptions: (name) => {
        var maps = dataMaps[name.group]
        var key = name.name
        var category = name.category
        if (!maps || !key || !category) return null
        for (const item of maps) {
          if (item.key == key && item.category == category) {
            return item.items
          }
        }
        return null
      },
    }

    return result
  })

  return p
}

const _instance = {
  apiOptions: _apiOptions,
  selectNames: _allNames,
  getSelectOptions: getSelectOptions,
  getMultiSelectOptions: getMultiSelectOptions,
}

_instance.install = (app) => {
  app.provide('selectCollection', _instance)
}

export default _instance
